﻿var url = require('url'),
    http = require('http'),
    httpServer = require('http-server'),
    httpProxy = require('http-proxy');


//set http_proxy=http://proxy.wdf.sap.corp:8080
//set https_proxy=%http_proxy%
//git config --global http.proxy %http_proxy%
//git config --global https.proxy %https_proxy%

// mkdir thirdparty\resources
//$\thirdparty\resources>mklink /J thirdparty ..\..\loader2\bower_components

// 
// Create a proxy server with custom application logic 
// 
var proxy = httpProxy.createProxyServer({});

// targets
var frontend = [
        { module: 'thirdparty', port: 15000 },
        { module: 'core', port: 15001 },
        { module: 'fin', port: 15002 },
        { module: 'launcher', port: 15003 }
],
    backend = { module: 'backend', uri: 'http://localhost:25001' };

frontend.forEach(function (it) {
    it.host = 'localhost';
    it.uri = 'http://' + it.host + ':' + it.port;
});


frontend.forEach(function (it) {
    httpServer.createServer({
        root: "../" + it.module,
        showDir: 'false'
    }).listen(it.port);

    console.log('listen ' + it.module + ' on port ' + it.port.toString());
});

// 
// Create your custom server and just call `proxy.web()` to proxy 
// a web request to the target passed in the options 
// also you can use `proxy.ws()` to proxy a websockets request 
// 
var server = http.createServer(function (req, res) {

    // probe the frontend
    if (req.method === 'GET') {
        var probe = 0,
            success = false;
        frontend.forEach(function (it) {
            //console.log('ready to ping -> ' + it);

            var host = it;
            http.request({ host: host.host, port: host.port, method: 'HEAD', path: req.url }, function (pres) {
                probe++;
                //pres.end();
                if (success) {
                    //console.log('request [' + req.url + '] was handled before -> ' + host);
                    return;
                } else if (pres.statusCode === 200) {
                    success = true;
                    console.log(host.uri + req.url + ' [' + host.module + ']');
                    proxy.web(req, res, { target: host.uri });
                } else if (probe === frontend.length) {
                    console.log(backend.uri + req.url + ' [' + backend.module + ']');
                    proxy.web(req, res, { target: backend.uri });
                } else {
                    //console.log('what? -> ' + probe);
                }
            }).end();
        });

        //console.log('leaving [GET] request to -> ' + req.url);
    } else {
        console.log(backend.uri + req.url + ' [' + backend.module + ']');
        proxy.web(req, res, { target: backend.uri });
    }

    //console.log('leaving request to -> ' + req.url);
});


console.log("listening on port 5050")
server.listen(5050);



// 
// fake backend
// 
http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('hello fake backend!' + '\n' + JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(25001);